<?php

/**
 *  登陆页
 * @file    
 * @date    
 * @author  
 * @version    
 */

namespace app\backend\controller;

use think\Request;
use think\Db;
use think\Session;
//use think\console\Command;
use think\Controller;
//use think\console\command\make\Controller;

class NormalAdministrator extends Controller {

    /**
     * 主页面
     */
    public function index() {
        $result=db("students")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }

    public function change_password() {
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        //判断原始密码是否正确
        $res=db('administrators')->field('password')->where('uid',session('user_id'))->find();
        $tem=$data['password0'].Session::get('user_name');
        if(MD5($tem)!=$res['password']){
            $this->error('修改失败，原始密码不正确!');
        }
        //判断两次密码是否一致
        if (!$data['password1']) {
            unset($data['password1']);
        } else {
            if ($data['password1'] != $data['password2']) {
                $this->error('两次密码不一致!');
            }
            $salt=session('user_name');
            $data['password1'] = md5($data['password1'].$salt);
        }
        $res = db('administrators')->where('uid',session('user_id'))->update(['password'=>$data['password1']]);
        if ($res) {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'修改密码','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('密码修改成功，请重新登录','Login/index');
        } else {
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'修改密码','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('修改失败');
        }
    }
    
    public function course_list(){
        $result=db("courses")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }

    public function stu_courses_list(){
        $result=db("stu_courses")->select();
        $this->assign('lists',$result);
        return $this->fetch();
    }

    public function course_select(){
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('students')->where('stu_id',$data['stu_id'])->find();
        if(!$res){
            $this->error('该学生不存在，选课失败！');
        }
        $res=db('courses')->where('course_id',$data['course_id'])->find();
        if(!$res){
            $this->error('该课程不存在，选课失败！');
        }
        if($res['course_nums']<1){
            $this->error('该课程人数已满，选课失败！');
        }
        $res=db('stu_courses')->where('course_id',$data['course_id'])->where('stu_id',$data['stu_id'])->find();
        if($res){
            $this->error('该学生已选择该课程，请勿重复选择');
        }
        $info=['stu_id'=>$data['stu_id'],'course_id'=>$data['course_id']];
        Db::startTrans();
        try{
            Db::table('stu_courses')->insert($info);
            Db::table('courses')->where('course_id',$info['course_id'])->setDec('course_nums');
            Db::commit(); // 提交事务
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'选课','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('选课成功'); //this->success() 这个函数的源码其实也是会抛出异常
        } catch (\think\Exception\DbException $e) {  //仅catch sql 异常
            Db::rollback(); // 回滚事务
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'选课','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('选课失败');
        }
    }
    
    public function course_exit() {
        $data = input();
        if(!$data){
            return $this->fetch();
        }
        $res=db('students')->where('stu_id',$data['stu_id'])->find();
        if(!$res){
            $this->error('该学生不存在，退课失败！');
        }
        $res=db('courses')->where('course_id',$data['course_id'])->find();
        if(!$res){
            $this->error('该课程不存在，退课失败！');
        }
        $res=db('stu_courses')->where('course_id',$data['course_id'])->where('stu_id',$data['stu_id'])->find();
        if(!$res){
            $this->error('该学生未选择该课程，退课失败');
        }
        Db::startTrans();
        try{
            Db::table('stu_courses')->where('course_id',$data['course_id'])->where('stu_id',$data['stu_id'])->delete();
            Db::table('courses')->where('course_id',$data['course_id'])->setInc('course_nums');
            // 提交事务
            Db::commit();
            //this->success() 这个函数的源码其实也是会抛出异常
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'退课','result'=>'成功','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->success('退课成功');
        } catch (\think\Exception\DbException $e) {  //仅catch sql 异常
            // 回滚事务
            Db::rollback();
            $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'退课','result'=>'失败','user'=>session('user_id')];
            db('logs')->insert($data);
            $this->error('退课失败');
        }
    }
    
    public function logout() {
        $data=['time'=>date('Y-m-d H:i:s',time()),'ip'=>request()->ip(),'event'=>'登出','result'=>'成功','user'=>session('user_name')];
        db('logs')->insert($data);
        session('user_name', null);
        session('user_id', null);
        $this->success('退出成功', 'login/index');
    }
    
}
